package com.register.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.register.pojo.SysUsers;
import com.register.service.impl.QRUserAndDepotServiceImpl;
import com.register.service.impl.UserServiceImpl;
import com.register.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userServiceImpl;
    @Autowired
    QRUserAndDepotServiceImpl qrUserAndDepotService;

    //查看用户列表
    @GetMapping("/list")
    public String list(Model model){

        List<SysUsers> lists =  userServiceImpl.list();
        model.addAttribute("result",Result.ok(lists));
        return "userList";
    }
    //添加用户
    @PostMapping("/add")
    @ResponseBody
    public Result list(@RequestBody SysUsers users){

        return userServiceImpl.addUser(users);
    }
    //删除用户
    @GetMapping("/delete")
    @ResponseBody
    public Result delete(Model model,int id){

        boolean flag = userServiceImpl.removeById(id);
        //删除场站与用户的对应关系
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userid",id);
        qrUserAndDepotService.remove(queryWrapper);
        if (flag == false){
            return Result.fail();
        }else{
            return  Result.ok();
        }
    }
    //修改用户信息
    @PostMapping("/update")
    @ResponseBody
    public Result update(Model model,@RequestBody SysUsers user){

        userServiceImpl.update(user);
        return Result.ok();
    }
}
